import { Message, Loading } from "element-ui";
import Vue from "vue";
import _ from "lodash";
import moment from "moment";
import JsonExcel from "vue-json-excel";
import { timestamp } from "@/utils/token";
// import "normalize.css"
Vue.prototype.$_ = _;
Vue.prototype.$moment = moment;
// moment.defineLocale('zh-cn', {
// relativeTime : {
// future :'%s内',

// past :'%s前',

// s :'几秒',

// m :'1 分钟',

// mm :'%d 分钟',

// h :'1 小时',

// hh :'%d 小时',

// d :'1 天',

// dd :'%d 天',

// M :'1 个月',

// MM :'%d 个月',

// y :'1 年',

// yy :'%d 年'

// },

// });

Vue.prototype.$headers = () => {
  return {
    timestamp: timestamp(),
    id: sessionStorage.getItem("userId") || "",
    code: sessionStorage.getItem("token") || "",
  };
};

// 导出表格
Vue.component("downloadExcel", JsonExcel);
// 异常提示
Vue.prototype.$error = (msg = "网络错误") => {
  Message.closeAll();
  Message({
    message: msg,
    type: "error",
  });
};
// 成功提示
Vue.prototype.$success = (msg = "加载中") => {
  Message.closeAll();
  Message({
    message: msg,
    type: "success",
  });
};
// 全局url
let {
  VUE_APP_IMAGE_UPLOAD,
  VUE_APP_URL_UPLOAD,
  VUE_APP_VIDEO_UPLOAD,
} = process.env;
Vue.prototype.$upload = VUE_APP_IMAGE_UPLOAD;
Vue.prototype.$uploadURL = VUE_APP_URL_UPLOAD;
Vue.prototype.$uniqueId = _.uniqueId;

Vue.prototype.$uploadVIDEO = VUE_APP_VIDEO_UPLOAD;
Vue.prototype.$pickerOptions = {
  disabledDate(time) {
    return time.getTime() > Date.now();
  },
};

// 下载文件
Vue.prototype.$downloadFiles = (url) => {
  console.log(url);
  let a = document.createElement("a");
  a.target = "_blank";
  a.href = url;
  document.body.appendChild(a);
  a.click(); //下载
  URL.revokeObjectURL(a.href); // 释放URL 对象
  document.body.removeChild(a); // 删除 a 标签
};
// 校验上传格式
Vue.prototype.$beforeAvatarUpload = (e, type) => {
  let check = [];
  if (type == 1) {
    check = [
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      "application/pdf",
      "application/vnd.ms-excel",
    ];
  } else if (type == 2) {
    check = ["video/mp4"];
  } else {
    check = ["image/png", "image/jpg", "image/jpeg"];
  }
  if (!check.includes(e.type)) {
    if (type == 1) {
      Message.error("请上传world、excel、pdf");
    } else if (type == 2) {
      Message.error("请上传mp4格式");
    } else {
      Message.error("请上传png、jpg图片");
    }

    return false;
  }
  if (type == 2) {
    if (e.size > 500 * 1024 * 1024) {
      Message.error("视频大小不超过500M");
      return false;
    }
  } else {
    if (e.size > 5 * 1024 * 1024) {
      Message.error("文件大小不超过5M");
      return false;
    }
  }
  //  Vue.prototype.$closeLoading=Loading.service({
  //         lock: true,
  //         text: '加载中',
  //         spinner: 'el-icon-loading',
  //         background: 'rgba(0, 0, 0, 0.7)'
  //       });
  // 获取图片上传名称
  Vue.prototype.$imageName = e.name;
};
